define(function(require,exports,module){
	require('uploadify');
	exports.init = function(){
		//异步加载添加页面
		ajax_load_food_add_page();
		//异步加载食品管理列表页
		ajax_load_food_list_page();
		choose_all();
	}	
	exports.operate = function(){
		//点击分页相关的
		food_page_link_click();

		//single  delete
		food_single_delete();
		//mutil_delete
		food_mutil_delete();

		//single forbidden
		food_single_forbidden();
		//mutil forbidden
		food_mutil_forbidden();

		//single recover
		food_single_recover();
		//mutil recover
		food_mutil_recover();

		//点击编辑时，加载编辑页面
		ajax_click_load_food_edit_page()
		//推荐商品
		ajax_recomment_food();
		//取消推荐
		ajax_un_recomment_food();
		//多项推荐
		ajax_mutil_recomment_food();
		//对象取消推荐
		ajax_mutil_un_recomment_food();

		//点击管理商品这个选项卡的时候，重新加载
		reload_food_list();

		//点击查询
		food_search_submit_click();
	}
	exports.win_resize = function(){

	}
	function ajax_load_food_add_page(){
		$.post(backend_var.ajaxGetAddFoodPageUrl,{},function(e){
				$(".food_add_div").html(e);
				//图片的灯箱效果
				food_img_fancybox("food_img_fancybox");
				//uploadify替换
				uploadify_instead("food_img_one");
				uploadify_instead("food_img_two");
				uploadify_instead("food_img_three");
				//添加富文本编辑器
				UE.getEditor('food_description');
				//确认按钮
				click_to_add();
		},'html');
	}
	function food_img_fancybox(img_class){
		$("."+img_class).live('click',function(){
			if($(this).attr('src') == ''){
				// $.Zebra_Dialog('您还没有点击下方的上传图片哦！', {
				// 	    'type':'information',
				// 	    'title':'提示',
				// 	    'buttons':  ['确定'],
				// 	    'onClose':  function() {
				// 	    }
				// });	
				$(this).attr('title','上传图片后，再点击试试？')	
				return false;
			}else{
				$(this).fancybox({
					aspectRatio:true,
					autoResize:true,
					closeClick:true,
					title:'图片若是太大，请拖动滚动条查看，有待改善！请谅解',
					afterClose:function(){
						$("."+img_class).show();
					},
				});				
			}
		})

	}
	function uploadify_instead(input_id){
		$("#"+input_id).uploadify({
	    	'fileTypeExts' : '*.gif; *.jpg; *.png',
	    	'buttonImage':backend_var.uploadify_buttonImg,
	        'swf'      : backend_var.uploadify_url,
	        'formData':{
	        	'asdfa':"adsasdf"
	        },
	        'itemTemplate':'',
	        'removeTimeout' : 100,
	        'uploader' : backend_var.ajaxUploadCookFoodImgUrl,
	        'onUploadSuccess' : function(file, e, response) {
           		var e = $.parseJSON(e);
           		$("img[name='"+input_id+"']").attr("title",'点击图片可预览');
           		$("input[name='"+input_id+"']").val(e.data['savename']);
           		$("img[name='"+input_id+"']").attr("src",backend_var.upload_url+"/"+e.data['savepath']+"/"+e.data['savename']);
				$.Zebra_Dialog('图片上传成功，请填写其他信息', {
				    'type':'information',
				    'title':'提示',
				    'buttons':  ['确定'],
				    'onClose':  function() {
				    }
				});          		
        	}
	    });
	}

	function click_to_add(){
		$(".food_button").live('click',function(){
			var data = $("form[name='food_form']").serializeArray();
			$.post(backend_var.ajaxAddFoodUrl,data,function(e){
				if(e.status){
					$.Zebra_Dialog(e.message, {
					    'type':'information',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});						
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});						
				}
			},'json');
		})
	}

	function ajax_load_food_list_page(){
		$.post(backend_var.ajaxLoadFoodListPageUrl+"/p/1",{},function(e){
			$(".food_replace_div").html(e);
			// food_list_img_fancybox();
		},'html');
	}
	function food_page_link_click(){
		$(".food_page_show a").live('click',function(){
			var a_class = $(this).attr('class');
			var page_num;
			if(a_class == 'num'){
				page_num = $(this).text();
			}
			if(a_class == 'next'){
				page_num = Number($(".food_page_show .current").text()) + 1;

			}
			if(a_class == 'prev'){
				page_num = $(".food_page_show .current").text() - 1;
			}
			//这里的'p'参数是与page分页类中的   //分页参数名 一致的
			var data = {
				t:$("form[name='food_search'] select[name='type']").val(),
				c:$("form[name='food_search'] input[name='search_content']").val()
			};
			$.post(backend_var.ajaxLoadFoodListPageUrl+"/p/"+page_num,data,function(html){
				$(".food_replace_div").html(html);
				// food_list_img_fancybox();
			},'html');
			return false;
		});
	}

	function food_search_submit_click(){
		$("input[name='food_search_submit']").live('click',function(){
			var data = {
				t:$("form[name='food_search'] select[name='type']").val(),
				c:$("form[name='food_search'] input[name='search_content']").val()
			};
			$.post(backend_var.ajaxLoadFoodListPageUrl+"/p/1",data,function(html){
				$(".food_replace_div").html(html);
				// food_list_img_fancybox();
			},'html');
		})
	}
	function food_list_img_fancybox(){
		$(".list_show_img").fancybox({
			aspectRatio:true,
			autoResize:true,
			closeClick:true,
			title:'图片若是太大，请拖动滚动条查看，有待改善！请谅解',
			afterClose:function(){
				$(".list_show_img").show();
			},
		});	
	}
	function choose_all(){
		$("input[name='food_all']").live('click',function(){
			var checkbox_status = $(this).attr("checked");
			if(checkbox_status == 'checked'){
				$("input[name='goods_id[]']").attr('checked','checked');
			}else{
				$("input[name='goods_id[]']").removeAttr('checked');
			}
		});
	}
	function food_single_delete(){
		$(".food_delete").live('click',function(){
			var obj = $(this);
			var goods_id = $(this).parents('tr').attr("data-id");
			var data = {
				goods_id:goods_id
			}
			$.post(backend_var.ajaxFoodDeleteUrl,data,function(e){
				obj.parents("tr").css("background",'red').fadeOut(400);
			},'json');
		})
	}
	function food_mutil_delete(){
		$(".food_multi_delete").live('click',function(){
			var data = $("form[name='food_mutil_operate']").serializeArray();
			$.post(backend_var.ajaxFoodDeleteUrl,data,function(e){
				for (var i = e.data.length - 1; i >= 0; i--) {
					$(".food_list tr[data-id="+e.data[i]+"]").fadeOut(400);
				};
			},'json');
		})
	}
	function food_single_forbidden(){
		$(".food_forbidden").live('click',function(){
			var obj = $(this);
			var goods_id = $(this).parents('tr').attr("data-id");
			var data = {
				goods_id:goods_id
			}
			$.post(backend_var.ajaxFoodForbiddenUrl,data,function(e){
				obj.text("恢复").removeClass("food_forbidden").addClass("food_forbidden_on");
			},'json');
		});
	}
	function food_mutil_forbidden(){
		$(".food_multi_forbidden").live('click',function(){
			var data = $("form[name='food_mutil_operate']").serializeArray();
			$.post(backend_var.ajaxFoodForbiddenUrl,data,function(e){
				for (var i = e.data.length - 1; i >= 0; i--) {
					$(".food_list tr[data-id="+e.data[i]+"] .food_forbidden").text("恢复").removeClass("food_forbidden").addClass("food_forbidden_on");
				};
			},'json');
		})
	}
	function food_single_recover(){
		$(".food_forbidden_on").live('click',function(){
			var obj = $(this);
			var goods_id = $(this).parents('tr').attr("data-id");
			var data = {
				goods_id:goods_id
			}
			$.post(backend_var.ajaxFoodRecoverUrl,data,function(e){
				obj.text("禁用").removeClass("food_forbidden_on").addClass("food_forbidden");
			},'json');
		});
	}
	function food_mutil_recover(){
		$(".food_multi_recover").live('click',function(){
			var data = $("form[name='food_mutil_operate']").serializeArray();
			$.post(backend_var.ajaxFoodRecoverUrl,data,function(e){
				for (var i = e.data.length - 1; i >= 0; i--) {
					$(".food_list tr[data-id="+e.data[i]+"] .food_forbidden_on").text("禁用").removeClass("food_forbidden_on").addClass("food_forbidden");
				};
			},'json');
		})
	}

	function ajax_recomment_food(){
		$(".food_recomment").live('click',function(){
			var obj = $(this);
			var goods_id = $(this).parents('tr').attr("data-id");
			var data = {
				goods_id:goods_id,
				recomment_data:'Y'
			}
			$.post(backend_var.ajaxFoodRecOrUnRecUrl,data,function(e){
				obj.text("取消推荐").removeClass("food_recomment").addClass("food_recomment_no");
			},'json');		
		})
	}
	function ajax_mutil_recomment_food(){
		$(".food_multi_recomment").live('click',function(){
			var data = $("form[name='food_mutil_operate']").serializeArray();
			data.push({name:'recomment_data',value:'Y'});
			$.post(backend_var.ajaxFoodRecOrUnRecUrl,data,function(e){
				for (var i = e.data.length - 1; i >= 0; i--) {
					$(".food_list tr[data-id="+e.data[i]+"] .food_recomment").text("取消推荐").removeClass("food_recomment").addClass("food_recomment_no");
				};
			},'json');
		})
	}

	function ajax_un_recomment_food(){
		$(".food_recomment_no").live('click',function(){
			var obj = $(this);
			var goods_id = $(this).parents('tr').attr("data-id");
			var data = {
				goods_id:goods_id,
				recomment_data:'N'
			}
			$.post(backend_var.ajaxFoodRecOrUnRecUrl,data,function(e){
				obj.text("推荐").removeClass("food_recomment_no").addClass("food_recomment");
			},'json');		
		})
	}
	function ajax_mutil_un_recomment_food(){
		$(".food_multi_recomment_no").live('click',function(){
			var data = $("form[name='food_mutil_operate']").serializeArray();
			data.push({name:'recomment_data',value:'N'});
			$.post(backend_var.ajaxFoodRecOrUnRecUrl,data,function(e){
				for (var i = e.data.length - 1; i >= 0; i--) {
					$(".food_list tr[data-id="+e.data[i]+"] .food_recomment_no").text("推荐").removeClass("food_recomment_no").addClass("food_recomment");
				};
			},'json');
		})		
	}
	function ajax_click_load_food_edit_page(){
		$(".food_edit").live('click',function(){
			var data = {
				goods_id:$(this).parents("tr").attr("data-id")
			};
			$.post(backend_var.ajaxGetFoodEditPageUrl,data,function(e){
				$(".food_edit_div").html(e);
				//页面加载完后，将相应的选项卡也选中
				$(".food_manage .main_card_content_ul li").removeClass("action-target-li");
				$(".food_manage .main_card_content_ul li[target-div=3]").addClass("action-target-li");
				$(".food_manage .target-div .target-son").removeClass("target-div-active");
				$(".food_manage .target-div .target-div-3").addClass("target-div-active");

				//加载灯箱效果
				food_img_fancybox('food_edit_img_fancybox');
				//加载uploadify
				uploadify_instead("food_edit_img_one");
				uploadify_instead("food_edit_img_two");
				uploadify_instead("food_edit_img_three");
				//加载富文本
				UE.getEditor('food_edit_description');
				//点击保存按钮
				click_to_save();
		},'html');
		})
	}
	function click_to_save(){
		$(".food_edit_button").live('click',function(){
			var data = $("form[name='food_eidt_form']").serializeArray();
			$.post(backend_var.ajaxSaveFoodEditUrl,data,function(e){
				if(e.status){
					$.Zebra_Dialog(e.message, {
					    'type':'information',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});						
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});						
				}
			},'json')
		})
	}
	function reload_food_list(){
		$(".food_manage .main_card_content_ul li[target-div=1]").live('click',function(){
			var pageNum = $(".food_page_show .current").text();
			$.get(backend_var.ajaxLoadFoodListPageUrl,{p:pageNum},function(e){
				$(".food_replace_div").html(e);
			},'html');
		})
	}
});